/*
 * @Author: your name
 * @Date: 2019-10-30 17:41:59
 * @LastEditTime: 2019-11-04 15:18:02
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \wwwroot\packs_vue\src\api\user.js
 */
// import request from '@/utils/request'
import axios from 'axios'
import config from '../config/config'

// const api_url = 'http://packs.test/api/';
// axios 配置
axios.defaults.timeout = 30000

// 测试地址
axios.defaults.baseURL = config.BASE_URL

// POST传参序列化
axios.interceptors.request.use(
  config => {
    config.headers['Content-Type'] = 'application/json'
    // 'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
    // if (config.method === 'post') {
    //   config.data = JSON.stringify(config.data)
    // }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
export function fetch(options) {
  return new Promise((resolve, reject) => {
    if (!options.method) {
      options.method = 'post'
    }
    axios(options)
      .then(
        response => {
          if (response.data.access_token) {
            localStorage.setItem('_token', response.data.access_token)
          }
          if (response.data.status_code === '401') {
            this.$notify.error({
              title: '错误',
              message: '网络异常！请退出登录！'
            })
            // delCookie('Admin-Token')
          }
          resolve(response.data)
        },
        err => {
          reject(err)
        }
      )
      .catch(error => {
        reject(error)
      })
  })
}
export function login(data) {
  return fetch({
    url: '/login',
    data: data
  })
}

export function getInfo(token) {
  return fetch({
    url: '/user/info'
    // method: 'get',
    // params: { token }
  })
}

export function logout() {
  return fetch({
    url: '/logout'
    // method: 'get',
    // params: { token }
  })
}
